package org.mybatis.generator.plugins;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.utils.JavaMapperGeneratorHelper;
import org.mybatis.generator.utils.XMLMapperGeneratorHelper;

import java.util.List;
import java.util.Set;

/**
 * <b><code>BatchDeletePlugin</code></b>
 * <p/>
 * Description: 批量删除
 * <p/>
 * <b>Creation Time:</b> 2020/06/10.
 */
public class BatchDeletePlugin extends PluginAdapter {

	private final static String BATCH_DELETE = "batchDelete";

	private final static String PARAMETER_NAME = "ids";

	@Override
	public boolean validate(List<String> list) {
		return true;
	}

	@Override
	public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass,
			IntrospectedTable introspectedTable) {
		if (introspectedTable.getTargetRuntime() == IntrospectedTable.TargetRuntime.MYBATIS3) {
			clientBatchDeleteMethodGenerated(interfaze, introspectedTable, context);
		}
		return super.clientGenerated(interfaze, topLevelClass, introspectedTable);
	}

	@Override
	public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
		if (introspectedTable.getTargetRuntime() == IntrospectedTable.TargetRuntime.MYBATIS3) {
			sqlMapBatchDeleteElementGenerated(document, introspectedTable);
		}
		return super.sqlMapDocumentGenerated(document, introspectedTable);
	}

	/**
	 * 批量删除的xml方法生成
	 * 
	 * @param document
	 * @param introspectedTable
	 */
	private void sqlMapBatchDeleteElementGenerated(Document document, IntrospectedTable introspectedTable) {

		String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime();

		String key = introspectedTable.getPrimaryKeyColumns().get(0).getActualColumnName();

		String baseSql = String.format("delete from %s where %s in (", tableName, key);

		XmlElement deleteElement = XMLMapperGeneratorHelper.baseElementGenerator(XMLMapperGeneratorHelper.DELETE,
				BATCH_DELETE, FullyQualifiedJavaType.getNewListInstance().getFullyQualifiedName(), null, null);

		XmlElement foreachElement = XMLMapperGeneratorHelper.baseForeachElementGenerator(PARAMETER_NAME, "item",
				"index", null);

		deleteElement.addElement(new TextElement(baseSql));

		foreachElement.addAttribute(new Attribute("separator", ","));

		foreachElement.addElement(new TextElement("#{item}"));

		deleteElement.addElement(foreachElement);

		deleteElement.addElement(new TextElement(")"));

		document.getRootElement().addElement(deleteElement);
	}

	/**
	 * 批量删除的mapper方法生成
	 * 
	 * @param document
	 * @param introspectedTable
	 */
	private void clientBatchDeleteMethodGenerated(Interface interfaze, IntrospectedTable introspectedTable,
			Context context) {
		Set<FullyQualifiedJavaType> importedTypes = JavaMapperGeneratorHelper
				.importedBaseTypesGenerator(introspectedTable);
		FullyQualifiedJavaType paramType = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType();
		Method batchDeleteMethod = JavaMapperGeneratorHelper.methodGenerator(BATCH_DELETE, JavaVisibility.DEFAULT,
				FullyQualifiedJavaType.getIntInstance(),
				new Parameter(new FullyQualifiedJavaType(paramType.getFullyQualifiedName() + "[]"), PARAMETER_NAME,
						"@Param(\"" + PARAMETER_NAME + "\")"));

		context.getCommentGenerator().addGeneralMethodComment(batchDeleteMethod, introspectedTable);
		interfaze.addImportedTypes(importedTypes);
		interfaze.addMethod(batchDeleteMethod);
	}
}